
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>codeop --- 编译Python代码 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="导入模块" href="modules.html" />
    <link rel="prev" title="code --- 解释器基础类" href="code.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/codeop.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="modules.html" title="导入模块"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="code.html" title="code --- 解释器基础类"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">自定义 Python 解释器</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-codeop">
<span id="codeop-compile-python-code"></span><h1><a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> --- 编译Python代码<a class="headerlink" href="#module-codeop" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/codeop.py">Lib/codeop.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> 模块提供了可以模拟Python读取-执行-打印循环的实用程序，就像在 <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code></a> 模块中一样。因此，您可能不希望直接使用该模块；如果你想在程序中包含这样一个循环，你可能需要使用 <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code></a> 模块。</p>
<p>这个任务有两个部分:</p>
<ol class="arabic simple">
<li><p>能够判断一行输入是否完成了一个Python语句：简而言之，告诉我们是否要打印 '<code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code>' 或 '<code class="docutils literal notranslate"><span class="pre">...</span></code>'。</p></li>
<li><p>记住用户已输入了哪些 future 语句，这样后续的输入可以在这些语句被启用的状态下被编译。</p></li>
</ol>
<p><a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> 模块提供了分别以及同时执行这两个部分的方式。</p>
<p>只执行前一部分：</p>
<dl class="function">
<dt id="codeop.compile_command">
<code class="sig-prename descclassname">codeop.</code><code class="sig-name descname">compile_command</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">filename=&quot;&lt;input&gt;&quot;</em>, <em class="sig-param">symbol=&quot;single&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#codeop.compile_command" title="永久链接至目标">¶</a></dt>
<dd><p>尝试编译 <em>source</em>，这应当是一个 Python 代码字符串，并且在 <em>source</em> 是有效的 Python 代码时返回一个代码对象。 在此情况下，代码对象的 filename 属性将为 <em>filename</em>，其默认值为 <code class="docutils literal notranslate"><span class="pre">'&lt;input&gt;'</span></code>。 如果 <em>source</em> <em>不是</em> 有效的 Python 代码而是有效的 Python 代码的一个前缀时将返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<p>如果 <em>source</em> 存在问题，将引发异常。 如果存在无效的 Python 语法将引发 <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>，而如果存在无效的字面值则将引发 <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 或 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<p><em>symbol</em> 参数确定 <em>source</em> 是作为一条语句 (对应默认值 <code class="docutils literal notranslate"><span class="pre">'single'</span></code>)，作为一系列语句 (<code class="docutils literal notranslate"><span class="pre">'exec'</span></code>) 还是作为一个 <a class="reference internal" href="../glossary.html#term-expression"><span class="xref std std-term">expression</span></a> (<code class="docutils literal notranslate"><span class="pre">'eval'</span></code>) 进行编译。 任何其他值都将导致引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>解析器有可能（但很不常见）会在到达源码结尾之前停止解析并成功输出结果；在这种情况下，末尾的符号可能会被忽略而不是引发错误。 例如，一个反斜杠加两个换行符之后可以跟随任何无意义的符号。 一旦解析器 API 得到改进将修正这个问题。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="codeop.Compile">
<em class="property">class </em><code class="sig-prename descclassname">codeop.</code><code class="sig-name descname">Compile</code><a class="headerlink" href="#codeop.Compile" title="永久链接至目标">¶</a></dt>
<dd><p>这个类的实例具有 <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> 方法，其签名与内置函数 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 相似，区别在于如果该实例编译了包含 <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 语句的程序文本，则实例会‘记住’并使用已生效的语句编译所有后续程序文本。</p>
</dd></dl>

<dl class="class">
<dt id="codeop.CommandCompiler">
<em class="property">class </em><code class="sig-prename descclassname">codeop.</code><code class="sig-name descname">CommandCompiler</code><a class="headerlink" href="#codeop.CommandCompiler" title="永久链接至目标">¶</a></dt>
<dd><p>这个类的实例具有 <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> 方法，其签名与 <a class="reference internal" href="#codeop.compile_command" title="codeop.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a> 相似；区别在于如果该实例编译了包含 <code class="docutils literal notranslate"><span class="pre">__future__</span></code> 语句的程序文本，则实例会‘记住’并使用已生效的语句编译编译所有后续程序文本。</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="code.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> --- 解释器基础类</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="modules.html"
                        title="下一章">导入模块</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/codeop.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="modules.html" title="导入模块"
             >下一页</a> |</li>
        <li class="right" >
          <a href="code.html" title="code --- 解释器基础类"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="custominterp.html" >自定义 Python 解释器</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>